package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import domain.Contact;
import domain.DAOContact;

/**
 * Servlet implementation class DeleteContact
 */
public class DeleteContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		DAOContact dao = (DAOContact) context.getBean("DaoContactBean");
		
		HashMap<String, String> parameter=new HashMap<String,String>();
		parameter.put("firstName", request.getParameter("firstName"));
		parameter.put("lastName", request.getParameter("lastName"));
		parameter.put("email", request.getParameter("email"));
		parameter.put("street", request.getParameter("street"));
		parameter.put("city", request.getParameter("city"));
		parameter.put("zip", request.getParameter("zip"));
		parameter.put("country", request.getParameter("country"));
		parameter.put("phone", request.getParameter("phone"));
		parameter.put("siret", request.getParameter("siret"));

		String amischeckBox = request.getParameter("amis");
		String famillecheckbox = request.getParameter("famille");
		String travailcheckBox = request.getParameter("travail");

		ArrayList<String> checkedBoxes = new ArrayList<String>();
		if (amischeckBox != null) {
			checkedBoxes.add(amischeckBox);
		}
		if (famillecheckbox != null) {
			checkedBoxes.add(famillecheckbox);
		}
		if (travailcheckBox != null) {
			checkedBoxes.add(travailcheckBox);
		}
		
		Contact[] listContact = null;
		try {
			listContact = dao.listContact(parameter, checkedBoxes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.setAttribute("listContact", listContact);
		
		RequestDispatcher rd = request
				.getRequestDispatcher("pages/deleteSearchResult.jsp");
		rd.forward(request, response);
	}

}
